package com.shaobufan.museum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaobufan.museum.mapper.CollectionsExhibitMapper;
import com.shaobufan.museum.mapper.CollectionsMapper;
import com.shaobufan.museum.pojo.entity.Collections;
import com.shaobufan.museum.pojo.entity.CollectionsExhibit;
import com.shaobufan.museum.pojo.vo.CategoryVo;
import com.shaobufan.museum.pojo.vo.CollectionsVo;
import com.shaobufan.museum.service.CategoryService;
import com.shaobufan.museum.service.CollectionsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 17126
 * &#064;description  针对表【museum_collections(馆藏表)】的数据库操作Service实现
 * &#064;createDate  2024-03-12 23:20:11
 */
@Service
public class CollectionsServiceImpl extends ServiceImpl<CollectionsMapper, Collections>
        implements CollectionsService {

    @Resource
    CollectionsMapper collectionsMapper;

    @Resource
    CategoryService categoryService;

    @Resource
    CollectionsExhibitMapper collectionsExhibitMapper;

    /**
     * 通过 id查询藏品
     *
     * @param collectionId 藏品 id
     * @return CollectionsVo
     */
    @Override
    public CollectionsVo searchMuseumCollectionsById(Integer collectionId) {
        QueryWrapper<Collections> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collection_id", collectionId);
        return getMuseumCollectionsVo(collectionsMapper.selectOne(queryWrapper));
    }

    /**
     * 模糊查询藏品
     *
     * @param collectionId   藏品id
     * @param categoryId     类别id
     * @param collectionEra  藏品年份
     * @param collectionName 藏品名称
     * @return museumCollectionsVoList 符合条件的藏品列表
     */
    @Override
    public List<CollectionsVo> searchMuseumCollections(Integer collectionId, Integer categoryId, String collectionEra, String collectionName) {
        QueryWrapper<Collections> queryWrapper = new QueryWrapper<>();

        if (ObjectUtils.isNotEmpty(collectionId)) {
            queryWrapper.eq("collection_id", collectionId);
        }
        if (ObjectUtils.isNotEmpty(categoryId)) {
            queryWrapper.eq("category_id", categoryId);
        }
        if (ObjectUtils.isNotEmpty(collectionEra)) {
            queryWrapper.eq("collection_era", collectionEra);
        }
        if (ObjectUtils.isNotEmpty(collectionName)) {
            queryWrapper.like("collection_name", collectionName);
        }
        List<Collections> collectionsList = collectionsMapper.selectList(queryWrapper);
        List<CollectionsVo> collectionsVoList;
        collectionsVoList = collectionsList.stream().map(this::getMuseumCollectionsVo).collect(Collectors.toList());
        return collectionsVoList;
    }

    /**
     * 插入新馆藏
     *
     * @param categoryId       类别 id
     * @param collectionEra    馆藏时代
     * @param collectionDetail 馆藏详情
     * @param collectionName   馆藏名称
     * @param collectionImage  馆藏图片
     * @return 插入数据的条数
     */
    @Override
    public Integer insertMuseumCollection(
            Integer categoryId,
            String collectionEra,
            String collectionDetail,
            String collectionName,
            String collectionImage
    ) {
        Collections collections = new Collections();
        collections.setCategoryId(categoryId);
        collections.setCollectionEra(collectionEra);
        collections.setCollectionDetail(collectionDetail);
        collections.setCollectionName(collectionName);
        collections.setCollectionImage(collectionImage);

        int insert = collectionsMapper.insert(collections);

        return insert;
    }

    /**
     * 更新藏品
     *
     * @param collectionId     藏品id
     * @param categoryId       类别id
     * @param collectionEra    藏品年代
     * @param collectionDetail 藏品详情
     * @param collectionName   藏品名称
     * @param collectionImage  藏品图片
     * @return 更新成功 1
     */
    @Override
    public Integer updateMuseumCollection(
            Integer collectionId,
            Integer categoryId,
            String collectionEra,
            String collectionDetail,
            String collectionName,
            String collectionImage
    ) {
        QueryWrapper<Collections> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collection_id", collectionId);

        Collections collections = new Collections();
        collections.setCategoryId(categoryId);
        collections.setCollectionEra(collectionEra);
        collections.setCollectionDetail(collectionDetail);
        collections.setCollectionName(collectionName);
        collections.setCollectionImage(collectionImage);

        int update = collectionsMapper.update(collections, queryWrapper);
        return update;
    }

    /**
     * 删除藏品
     *
     * @param collectionId 馆藏id
     * @return deleteById
     */
    @Override
    public Integer deleteMuseumCollectionById(Integer collectionId) {
        QueryWrapper<CollectionsExhibit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collection_id",collectionId);
        collectionsExhibitMapper.delete(queryWrapper);
        int deleteById;
        deleteById = collectionsMapper.deleteById(collectionId);
        return deleteById;
    }

    /**
     * @param collections 藏品实体
     * @return museumCollectionsVo 藏品实体Vo
     */
    @Override
    public CollectionsVo getMuseumCollectionsVo(Collections collections) {

        CategoryVo categoryVo = categoryService.searchCategoryById(collections.getCategoryId());

        CollectionsVo collectionsVo = new CollectionsVo();
        collectionsVo.setCollectionId(collections.getCollectionId());
        collectionsVo.setCategoryId(collections.getCategoryId());
        collectionsVo.setCategoryDetail(categoryVo.getCategoryDetail());
        collectionsVo.setCollectionEra(collections.getCollectionEra());
        collectionsVo.setCollectionDetail(collections.getCollectionDetail());
        collectionsVo.setCollectionName(collections.getCollectionName());
        collectionsVo.setCollectionImage(collections.getCollectionImage());
        return collectionsVo;
    }
}




